﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//Dk

public class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T> {
    public static T Instance {
        get {
            if (_instance == null) {
                var singList = Transform.FindObjectsOfType<T> ();
                if (singList.Length > 0) _instance = singList[0];
                else if (singList.Length <= 0) Debug.LogError ($"当前实例{typeof(T).Name}不存在");
                for (int i = 1; i < singList.Length; i++) Destroy (singList[i].gameObject);
            }
            return _instance;
        }
    }
    private static T _instance;
    private void Awake () {
        if (Instance != null && Instance != this) { Destroy (this.gameObject); return; }
        _instance = this as T;
        OnAwake ();
    }
    protected virtual void OnAwake () { }
}